﻿using ConfExpert.Forms.Base;
using ConfExpert.Utility;
using DevExpress.Utils;
using DevTeam;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace ConfExpert.Forms.Rapor.YeniYapi
{
    public partial class GenelStokMusteriSezon : XForm
    {
        string kirilim;
        public GenelStokMusteriSezon(string kirilim)
        {
            InitializeComponent();
            this.kirilim = kirilim;
        }

        BindingSource bs;
        private void GenelStokMusteriSezon_Load(object sender, EventArgs e)
        {
            if (kirilim.Contains("Customer"))
            {
                this.Text = "Stoklar. Müşteri-Sezon";
                gridView1.Init("GetMusteriSezon");
            }
            else if (kirilim.Contains("Vendor"))
            {
                this.Text = "Stoklar. Üretici-Sezon";
                gridView1.Init("GetUreticiSezon");
            }

            bs = gridView1.AddDetailGrid(gridView2, kirilim);

            gridView1.YanYanaGetirSagina(kirilim, "18");
            Dictionary<int,string>sezonlar = new Dictionary<int,string>();
            List<string> sezoncols = new List<string>();
            sezoncols.Add("9999");

            foreach(DataRow r in AppSession.Lookups.Sezon.Rows)
                sezonlar.Add(r["SezonId"].ToInt(), r["Name"].ToStr());

            foreach (DataColumn col in gridView1.DataTable().Columns)
                if (col.ColumnName.ToInt() > 0)
                    if (col.ColumnName.ToInt() > 999)
                        gridView1.SetCaptions(col.ColumnName + ":GenelToplam");
                    else
                    {
                        gridView1.SetCaptions(col.ColumnName + ":" + sezonlar[col.ColumnName.ToInt()]);
                        sezoncols.Add(col.ColumnName);
                    }

            gridView1.FormatAsNumber(0, sezoncols.ToArray());
            gridView2.FormatAsNumber(1, "Qty");
            gridView1.AddSummary(sezoncols.ToArray());
            gridView2.AddSummary("Qty");

            gridView1.MakeReadOnly();
            gridView2.MakeReadOnly();

            gridView1.FocusedColumnChanged += gridView1_FocusedColumnChanged;
            gridView1_FocusedColumnChanged(null, null);

            gridView1.RowCellStyle += gridView1_RowCellStyle;
            gridView1.BestFitColumns();
            gridView2.BestFitColumns();

            splitContainerControl2.AutoSize();
        }

        void gridView1_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
            if (e.Column.FieldName == "9999" || e.Column.FieldName == kirilim)
                e.Appearance.Font = new Font(AppearanceObject.DefaultFont, FontStyle.Bold);
        }

        void gridView1_FocusedColumnChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedColumnChangedEventArgs e)
        {
            string sezon = gridView1.FocusedColumn.FieldName;
            if (sezon.ToInt() > 0)
                bs.Filter = "SezonId=" + sezon;
            else
                bs.Filter = "SezonId = 0";

            gridView2.BestFitColumns();
        }
    }
}
